<template>
  <div class="container">
    <div class="header">
      <el-form :inline="true" :model="pageParams" class="demo-form-inline">
        <el-form-item label="点位搜索：">
          <el-input v-model="pageParams.name" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="区域搜索">
          <el-select v-model="searchForm.status" clearable placeholder="请选择">
            <el-option label="待办" value="1" />
            <el-option label="进行" value="2" />
            <el-option label="取消" value="3" />
            <el-option label="完成" value="4" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit"><i class="el-icon-search" />  查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="app-container">

      <div class="role-operate">
        <el-button class="newBuild" type="warning" @click="showAddDialog=true">
          <i class="el-icon-circle-plus-outline" />
          新建</el-button>
      </div>
      <!-- 新建组件 -->
      <addNode
        ref="addNode"
        :show-add-dialog.sync="showAddDialog"
        :row="Row"
        @addNode="getNodeDetail"
      />

      <el-table :data="tableData">
        <!-- 放置列 -->
        <el-table-column prop="index" align="center" width="100px" label="序号" />
        <el-table-column prop="name" align="center" width="200px" label="点位名称" />
        <el-table-column prop="region.name" align="center" width="200px" label="所在区域" />
        <el-table-column prop="businessType.name" align="center" width="150px" label="商圈类型" />
        <el-table-column prop="ownerName" align="center" width="200px" label="合作商" />
        <el-table-column prop="addr" align="center" label="详细地址" />
        <el-table-column align="center" label="操作">
          <!-- 放置操作按钮 -->
          <template v-slot="{ row }">
            <el-button
              type="text"
              @click="details(row.id)"
            >查看详情</el-button>
            <el-button
              type="text"
              @click="change(row,row.id)"
            >修改</el-button>
            <el-button
              type="text"
              style="color:red"
              @click="del(row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 查看详情组件 -->
      <el-dialog
        title="点位详情"
        :visible.sync="showdetailDialog"
        width="40%"
        style="font-weight: 700;"
        class="aera"
      >
        <div class="node">
          <template>
            <el-table
              :data="NodeList"
              style="width: 100%;"
            >
              <el-table-column
                prop="index"
                label="序号"
                width="70"
              />
              <el-table-column
                prop="innerCode"
                label="机器编号"
                width="180"
              />
              <el-table-column
                prop="vmStatus"
                label="设备状态"
              >
                <template v-slot="{ row }">
                  <span>  {{ row.vmStatus === 0 ? "未投放" : row.vmStatus === 1? '运营':'撤机' }} </span>
                </template>
              </el-table-column>
              <el-table-column
                prop="lastSupplyTime"
                label="最后一次供货时间"
              />
            </el-table>
          </template>
        </div>
      </el-dialog>
      <div class="footer">
        <span>共{{ res.totalCount }}条记录  第 {{ res.pageIndex }}/{{ res.totalPage }} 页</span>
        <el-row type="flex" style="height:60px" align="middle" justify="end">
          <el-button :disabled="res.pageIndex <= 1" @click="redNum">上一页</el-button>
          <el-button :disabled="res.pageIndex===res.totalPage" @click="addNum">下一页</el-button>
        </el-row>
      </div>
    </div>

  </div>
</template>

<script>
import addNode from '@/views/node/components/addNode.vue'
import { getNodeDetail, LookDetail, delNode } from '@/api/node'
export default {
  name: 'Region',
  components: {
    addNode
  },
  data() {
    return {
      NodeList: [],
      tableData: [],
      showAddDialog: false,
      showdetailDialog: false,
      pageParams: {
        pageIndex: 1,
        pageSize: 10,
        name: ''
      },
      res: '',
      searchForm: {
        taskCode: '',
        status: ''
      },
      Row: {}
    }
  },
  async created() {
    // this.getAreaList()
    this.getNodeDetail()
  },
  methods: {
    async del(id) {
      await delNode(id)
      this.$message.success('删除成功')
      this.getNodeDetail()
    },
    closeDialog() {
      this.showAddDialog = false
    },
    async onSubmit() {
      // this.getAreaList()
      this.getNodeDetail()
    },
    async details(id) {
      this.showdetailDialog = true
      const res = await LookDetail(id)
      // console.log(res)
      this.NodeList = res
      this.NodeList.forEach((item, index) => { // 序号调整
        this.$set(item, 'index', this.pageParams.pageSize * (this.pageParams.pageIndex - 1) + index + 1)
      })
    },
    async change(row, id) {
      // console.log(id)
      this.Row = row
      this.$nextTick(() => {
        this.$refs.addNode.changeRegion()
      })
      this.showAddDialog = true
    },
    // 获取点位管理列表
    async getNodeDetail() {
      const res = await getNodeDetail(
        this.pageParams.pageIndex,
        this.pageParams.pageSize
      )
      // res.currentPageRecords.forEach(item => {
      //   item.addr = item.addr.split('-').pop()
      // })
      // console.log(res.currentPageRecords) // 截取数组对象的addr的最后一个字符串
      // console.log(res)
      this.res = res
      this.tableData = res.currentPageRecords
      this.tableData.forEach((item, index) => { // 序号调整
        this.$set(item, 'index', this.pageParams.pageSize * (this.pageParams.pageIndex - 1) + index + 1)
      })
    },
    showAdd() {
      this.showAddDialog = true
    },
    addNum() {
      this.pageParams.pageIndex++
      this.getNodeDetail()
    },
    redNum() {
      this.pageParams.pageIndex--
      this.getNodeDetail()
    }
  }
}
</script>
<style lang="scss" scoped>
.container{
  width: 100%;
  height: 1050px;
  padding-left: 20px;
      .header{
    display: flex;
    height: 64px;
    align-items: center;
    margin-bottom: 20px;
    padding-left: 17px;
    background-color: #fff;
    margin-top: 20px;
    .demo-form-inline{
      margin-top: 20px;
    }
    }
  .app-container{
      width: 100%;
      height: 900px;
      .role-operate{
        margin-bottom: 20px;
        .newBuild{
          width: 80px!important;
          height: 36px;
          padding: 0;
          background: linear-gradient(135deg,#ff9743,#ff5e20)!important;
          border: none;
          margin-left: 18px;
          margin-top: 20px;
        }
        .gdConfig{
          width: 80px!important;
          height: 36px;
          padding: 0;
          background-color: #fbf4f0!important;
          border: none;
          color: #655b56!important;
        }
      }
      .footer{
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 32px 26px;
        span{
          font-size: 16px!important;
          color: #dbdfe5!important;
        }
        .el-button{
          display: flex;
          justify-content: center;
          align-items: center;
          width: 70px;
          height: 32px;
          margin: 0 16px;
          border-radius: 2px;
          background-color: #d5ddf8
        }
      }
      .dialogBody{
        display: flex;
        align-items: center;
        height: 36px;
        .diaText{
          font-size: 14px;
          font-weight: normal;
          color: #606266;
          margin-left: 20px;
        }
        .diaRight input{
            display: flex;
            width: 356px;
            height: 36px;
            line-height: 36px;
            margin-left: -40px;
            border-color: #d8dde3;
            border-radius: 4px;
            border: 1px solid #d8dde3;
            text-align: center;
        }
      }
      .node{
        font-weight: normal;
        margin-top: -20px;
      }
  }
}
::v-deep .el-dialog{
  border-radius: 10px;
  }
</style>
